package geppetto.reordering.graph;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;

import java.util.HashMap;

public class NumberOfPathsSparseMatrix {
	protected HashMap<ExtractedPhrasePair, HashMap<ExtractedPhrasePair, Double>> pathsFromAToB = new HashMap<ExtractedPhrasePair, HashMap<ExtractedPhrasePair,Double>>();
	protected HashMap<ExtractedPhrasePair, HashMap<ExtractedPhrasePair, Double>> pathsFromAToBInverse = new HashMap<ExtractedPhrasePair, HashMap<ExtractedPhrasePair,Double>>(); //simply changes the order of access
	
	public void increment(ExtractedPhrasePair A, ExtractedPhrasePair B, Double value){
		if(!pathsFromAToB.containsKey(A)){
			pathsFromAToB.put(A, new HashMap<ExtractedPhrasePair, Double>());
		}
		HashMap<ExtractedPhrasePair, Double> pathsFromA = pathsFromAToB.get(A);
		if(!pathsFromA.containsKey(B)){
			pathsFromA.put(B, 0d);
		}
		pathsFromA.put(B, pathsFromA.get(B) + value);
		
		if(!pathsFromAToBInverse.containsKey(B)){
			pathsFromAToBInverse.put(B, new HashMap<ExtractedPhrasePair, Double>());
		}
		HashMap<ExtractedPhrasePair, Double> pathsToB = pathsFromAToBInverse.get(B);
		if(!pathsToB.containsKey(A)){
			pathsToB.put(A, 0d);
		}
		pathsToB.put(A, pathsToB.get(A) + value);
	}
	
	public Double get(ExtractedPhrasePair A, ExtractedPhrasePair B){
		if(!pathsFromAToB.containsKey(A)){
			return 0d;
		}
		HashMap<ExtractedPhrasePair, Double> pathsFromA = pathsFromAToB.get(A);
		if(!pathsFromA.containsKey(B)){
			return 0d;
		}
		return pathsFromA.get(B);
	}
	
	public HashMap<ExtractedPhrasePair, Double> getPathsFromA(ExtractedPhrasePair A){
		if(!pathsFromAToB.containsKey(A)){
			return new HashMap<ExtractedPhrasePair, Double>();
		}
		return pathsFromAToB.get(A);
	}
	
	public HashMap<ExtractedPhrasePair, Double> getPathsToA(ExtractedPhrasePair B){
		if(!pathsFromAToBInverse.containsKey(B)){
			return new HashMap<ExtractedPhrasePair, Double>();
		}
		return pathsFromAToBInverse.get(B);
	}
	
}
